package com.example.areaclean;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.areaclean.dao.Classroom;
import com.example.areaclean.database.Student;

import org.json.JSONObject;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class stu_area_activity extends AppCompatActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    RequestQueue queue;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_area);

        queue= Volley.newRequestQueue(getApplicationContext());
        TextView textView=findViewById(R.id.txt_area);
        TextView textView2=findViewById(R.id.txt_num_need);
        linearLayout=findViewById(R.id.stu_layout);



        pref=getSharedPreferences("data",MODE_PRIVATE);
        jsonRequest(pref.getString("userNo",""));


    }

    public void jsonRequest(final String s_no){//通过userNo 查询student信息
        String url="http://192.168.31.32/tp/public/index.php/api/stu_area";

        HashMap<String, String> params = new HashMap<>();
         params.put("s_no",s_no);
        org.json.JSONObject jsonObject = new org.json.JSONObject(params);
        Log.d("Tag",jsonObject.toString());

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Classroom classroom= JSON.parseObject(jsonObject.toString(),Classroom.class);//解析到classroom
                        Log.d("TAG1",jsonObject.toString());
                        //java判断字符串相等不能用“==”
                        //存储到pref
                        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                        editor.putString("Stu_area",classroom.getStu_area());
                        editor.putString("Num_need",classroom.getNum_need());
                        editor.putString("S_name",classroom.getS_name());
                        editor.putString("S_class",classroom.getS_class());
                        editor.putString("Week",classroom.getWeek());
                        editor.apply();

                        TextView textView3 =(TextView)findViewById(R.id.txt_area);
                        textView3.setText(classroom.getStu_area());//textView在网络请求完毕后，更新到ui
                        TextView textView2=findViewById(R.id.txt_num_need);
                        textView2.setText(classroom.getNum_need());

                        String s_names[];
                        s_names=classroom.getS_name().split("#");
                        Log.d("EEEEE", s_names[0]+s_names[1]+s_names[2]);
                        String s_classes[];
                        s_classes=classroom.getS_class().split("#");
                        Log.d("EEEEE", s_names.length+"");

                        for (int i=0;i<s_names.length;i++){
                            View child = linearLayout.getChildAt(i);
                            if (child instanceof TextView){
                                TextView textView = (TextView) child;
                                textView.setVisibility('0');
                                textView.setText(s_names[i]+"                   "+s_classes[i]+"                "+classroom.getWeek());
                            }
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError"+volleyError);
                    }
                });
        queue.add(jsObjRequest);
                    }
                }
//        new Response.Listener<org.json.JSONObject>() {
//                    @Override
//                    public void onResponse(org.json.JSONObject jsonObject) {
//                        Classroom classroom= JSON.parseObject(jsonObject.toString(),Classroom.class);//解析到classroom
//                        Log.d("TAG1",jsonObject.toString());
//                        //java判断字符串相等不能用“==”
//                        //存储到pref
//                        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
//                        editor.putString("Stu_area",classroom.getStu_area());
//                        editor.putString("Num_need",classroom.getNum_need());
//                        editor.putString("S_name",classroom.getS_name());
//                        editor.putString("S_class",classroom.getS_class());
//                        editor.putString("Week",classroom.getWeek());
//                        editor.apply();
//
//                        TextView textView3 =(TextView)findViewById(R.id.txt_area);
//                        textView3.setText(classroom.getStu_area()+" !");//textView在网络请求完毕后，更新到ui
//
//                    }
//                },
//                new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError volleyError) {
//                        Log.d("TAG","fuckError"+volleyError);
//                    }
//                });
//        queue.add(jsObjRequest);


